// ** React Imports
import { useEffect, useState } from 'react'

// ** Axios Import
import axios from 'axios'

// ** Config
import authConfig from 'src/configs/auth'
import { useRouter } from 'next/router'

// ** Type Import
import { VerticalNavItemsType } from 'src/@core/layouts/types'

const ServerSideNavItems = () => {
  // ** State
  const [menuItems, setMenuItems] = useState<VerticalNavItemsType>([])
  const backEndApi = "auth/menus.php"
  const router = useRouter()

  useEffect(() => {
    const storedToken = window.localStorage.getItem(authConfig.storageTokenKeyName)!
    axios.get(authConfig.backEndApiHost + backEndApi, { headers: { Authorization: storedToken } }).then(response => {
      const menuArray = response.data
      if (menuArray && menuArray.status && menuArray.status == "ERROR" && router.pathname != '/login') {
        console.log("menuArray ERROR", menuArray, router)
        router.push('/login')
      }
      if(menuArray) {
        setMenuItems(menuArray)
      }
    })
  }, [])

  return { menuItems }
}

export default ServerSideNavItems
